package com.tablefood.dao.Impl;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.tablefood.dao.OrderDetailDao;
import com.tablefood.domain.ShopCart;
import com.tablefood.domain.ShopOrderDetail;

public class OrderDetailDaoImpl implements OrderDetailDao {

	@Resource
	private SessionFactory sessionFactory; 
	
	@Override
	public ShopOrderDetail insertOrderDetail(ShopOrderDetail orderDetail) {
		System.out.println("插入部分"); 
		sessionFactory.getCurrentSession().save(orderDetail);  
		System.out.println("插入结束");
		return orderDetail;
	}

	@Override
	public ShopOrderDetail updateOrderDetail(ShopOrderDetail orderDetail) {
		System.out.println("更新开始");
		sessionFactory.getCurrentSession().update(orderDetail);
		System.out.println("更新结束");
		return orderDetail;
	}

	/*
	 * 订单-订单详细
	 * 根据orderid和productid找detailid
	 * 找一个订单详细
	 * 1.存在-将商品数量更新
	 * 2.不存在-新建一个订单详细
	 * */
	@Override
	public List searchOrderDetail(String orderid,String productid) {
		
		String sql = "from ShopOrderDetail as orderDetail where orderDetail.shopOrder.orderid=?and orderDetail.shopProduct.productid=?";
		 
		System.out.println("查找用户基本语句："+sql);
		
		Query query = sessionFactory.getCurrentSession().createQuery(sql); 
		query.setString(0, orderid);
	    query.setString(1,productid);
		System.out.println("---查询结果----");
		System.out.println("是否有结果query.list().size()："+query.list().size()); 
		if(query.list().size()!=0){
			
			System.out.println("返回result");
			List result = query.list(); 
			return result;
			
		}else{
			System.out.println("没有数据");
			return null ;
		}
	}


	/*
	 * 根据订单详细id，查询某一个详细的所有内容
	 * */
	@Override
	public List searchOrderDetailById(String detailid) {
		
		String sql = "from ShopOrderDetail as orderDetail ,ShopOrder as Order ,ShopProduct as product where orderDetail.detailid=?";
		 
		System.out.println("根据订单详细id，查询某一个详细的所有内容:"+sql);
		
		Query query = sessionFactory.getCurrentSession().createQuery(sql); 
		query.setString(0, detailid);
	    
		System.out.println("---查询结果----");
		System.out.println("是否有结果query.list().size()："+query.list().size()); 
		if(query.list().size()!=0){ 
			System.out.println("返回result");
			List result = query.list(); 
			return result;
			
		}else{
			System.out.println("没有数据");
			return null ;
		}
	}

	/*
	 * 查询购物车
	 * 订单-订单详细
	 * 根据orderid找detailid 查询所有的详细
	 * */
	@Override
	public List searchAllOrderDetail(String orderid) {
	
		//商品id 名字 单价 购买数量 封面 库存量
		//String sql = "select a.shopProduct.productid ,a.productnum,a.price,b.cover,b.num,b.title from ShopOrderDetail as a ,ShopProduct as b ,ShopOrder as c where a.shopProduct.productid = b.productid and a.shopOrder.orderid = c.orderid and c.orderid=?";
		String sql = "from ShopOrderDetail as a where a.shopOrder.orderid=?";
		//String sql = "select a.productid ,a.productnum,a.price,b.cover,b.num,b.title,c.userid from shop_order_detail as a ,shop_product as b ,shop_order as c where a.productid = b.productid and a.orderid = c.orderid and c.orderid="+orderid;
		System.out.println("查找所有关联的订单详细："+sql);
		
 	    Query query = sessionFactory.getCurrentSession().createQuery(sql);
		//createQuery(sql); 
		query.setString(0, orderid);
	    
		System.out.println("---查询结果----");
		System.out.println("是否有结果query.list().size()："+query.list().size()); 
		if(query.list().size()!=0){
			System.out.println("返回result");
			List<ShopOrderDetail> result = query.list(); 
			return result;
			
		}else{
			System.out.println("没有数据");
			return null ;
		}
		
		 
	}

	/**   
	 * 查找某一商品详细   
	 * @param productid 商品ID
	 *  
	 * @return ShopOrderDetail 一个商品详细
	 */     
	@Override
	public ShopOrderDetail searchOneByProductid(String productid) {
		
		String sql = "from ShopOrderDetail as a where a.shopProduct.productid=?";
		Query query = sessionFactory.getCurrentSession().createQuery(sql);
		query.setString(0, productid);
		if(query.list().size()>0){
			List<ShopOrderDetail> list = query.list();
			ShopOrderDetail detail = list.get(0);
			return detail;
		}else
			return null;
	}

	@Override
	public boolean delOneDetail(Long detailid) {
		
		try{
			String sql = "delete from ShopOrderDetail as a where a.detailid=";
	        sessionFactory.getCurrentSession().createQuery(sql+detailid).executeUpdate();
	        return true;
        }catch(Exception e){
        	e.printStackTrace();
        	return false;
        }
		
	}

}
